package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Education;
import com.zpark.resume.entity.Recommend;
import com.zpark.resume.exception.DeleteEducationLoseException;
import com.zpark.resume.exception.FindCvByIdIsNullException;
import com.zpark.resume.exception.InsertEducationLoseException;
import com.zpark.resume.exception.UpdateEducationLoseException;
import com.zpark.resume.mapper.IEducationMapper;
import com.zpark.resume.service.IEducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: EducationServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/6/1  17:51
 */
@Service
public class EducationServiceImpl implements IEducationService {

    @Autowired
    private IEducationMapper educationMapper;

    /**
     * 根据cv的id查询数据
     * @param cvId
     * @return
     */
    @Override
    public List<Education> findByCvId(Integer cvId) {
        // 调用持久层接口查询数据
        List<Education> educations = educationMapper.selectByCvId(cvId);
        // 判断查询结果
        if (educations.size() <= 0) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return educations;
    }

    @Override
    public Integer addEducation(Education education) {
        // 判断education是否为null
        if (education == null) {
            throw new InsertEducationLoseException("添加教育经历失败");
        }
        // 调用持久层接口添加数据
        Integer row = educationMapper.insertEducation(education);
        // 判断执行结果
        if (row <= 0) {
            throw new InsertEducationLoseException("添加教育经历失败");
        }
        // 返回受影响的行数
        return row;
    }

    @Override
    public Integer changeEducationById(Education education) {
        // 判断education是否为null
        if (education == null) {
            throw new UpdateEducationLoseException("修改教育经历失败");
        }
        // 调用持久层接口修改数据
        Integer row = educationMapper.updateEducationById(education);
        // 判断执行结果
        if (row <= 0) {
            throw new UpdateEducationLoseException("修改教育经历失败");
        }
        // 返回受影响的行数
        return row;
    }

    @Override
    public Integer removeEducationById(Integer id) {
        // 判断id是否为null或者id=0
        if (id == null || id ==0) {
            throw new DeleteEducationLoseException("删除教育经历失败");
        }
        // 调用持久层接口删除数据
        Integer row = educationMapper.deleteEducationById(id);
        // 判断执行结果
        if (row <= 0) {
            throw new InsertEducationLoseException("添加教育经历失败");
        }
        // 返回受影响的行数
        return row;
    }
}
